<template>
	<view class="page">
		<nav-bar title="资讯详情">
			<!-- #ifdef APP-PLUS -->
			<view class="nav_right_info" slot="right">
				<button class="share" @click="onShare"></button>
			</view>
			<!-- #endif -->
		</nav-bar>
        <!-- 公共组件-每个页面必须引入 -->
        <public-module></public-module>
		<view class="title">{{newsInfo.title}}</view>
		<view class="news_info">
			<view class="left_info">
				<text>{{newsInfo.createTime}}</text>
				<text class="browse_num">{{newsInfo.numVisit}}</text>
			</view>
			<text>{{newsInfo.sourceName}}</text>
		</view>
		<view class="content">
			<jyf-parser ref="article"></jyf-parser>
		</view>
		<view class="comment_title" id="commentTitle">评论（{{commentCount}}）</view>
		<view class="comment_list_box">
			<view class="comment_list" v-for="(item,index) in commentList" :key="index">
				<image :src="item.userImg" mode="aspectFill"></image>
				<view class="comment_info">
					<view class="name_like">
						<view class="name">{{item.userName}}</view>
						<view class="like" @click="onLike(item,false)" :class="{active:item.isLike}">{{item.numLike || 0}}</view>
					</view>
					<view class="comment_content">{{item.content}}</view>
					<view class="time_reply">
						<view class="time">{{item.createTime}}</view>
						<view class="reply" @click="onReply(item)">{{item.replyCount}}回复</view>
					</view>
				</view>
			</view>
		</view>
		<view class="comment_send_box">
			<view class="send_input_box">
				<image src="../../static/icon/ic_Input.png" mode="aspectFit"></image>
				<input type="text" :placeholder="placeholder" v-model="sendContent" @confirm="onSendComment" />
			</view>
			<view class="icon_box" @click="onSlideComment">
				<image src="../../static/icon/ic_comment.png" mode="aspectFit"></image>
				<text>{{commentCount}}</text>
			</view>
			<view class="icon_box" @click="onLike(newsInfo,true)">
				<image v-if="newsInfo.isLike" src="../../static/icon/ic_up_c.png" mode="aspectFit"></image>
				<image v-else src="../../static/icon/ic_up.png" mode="aspectFit"></image>
			</view>
			<!-- #ifdef APP-PLUS -->
			<view class="icon_box" @click="onShare">
				<image src="../../static/icon/ic_share.png" mode="aspectFit"></image>
			</view>
			<!-- #endif -->
		</view>
		<z-popup v-model="popupShow" @on-hide="onPopupHide">
			<view class="reply_popup_box">
				<view class="reply_popup_title">{{replyCount}}条回复</view>
				<view class="reply_popup_close" @click="popupShow = false">
					<image src="../../static/icon/ic_close.png" mode="aspectFit"></image>
				</view>
				<scroll-view scroll-y="true" class="popup_scroll" @scrolltolower="getReplyList">
					<view class="popup_scroll_view">
						<view class="comment_list_box">
							<view class="comment_list">
								<image :src="popupCommentInfo.userImg" mode="aspectFill"></image>
								<view class="comment_info">
									<view class="name_like">
										<view class="name">{{popupCommentInfo.userName}}</view>
									</view>
									<view class="comment_content">{{popupCommentInfo.content}}</view>
									<view class="time_box">{{popupCommentInfo.createTime}}</view>
									<view class="like_reply">
										<view class="reply_box" v-if="popupCommentInfo.likeUserImgs">
											<image class="image" v-for="(item,index) in popupCommentInfo.likeUserImgs" :key="index" :src="item" mode="aspectFill"></image>
											<text>{{popupCommentInfo.numLike}}人赞过</text>
											<image class="arrow" src="../../static/icon/icon_enter.png" mode="aspectFit"></image>
										</view>
										<view class="like" :class="{active:popupCommentInfo.isLike}" @click="onLike(popupCommentInfo,false)">{{popupCommentInfo.numLike}}</view>
									</view>
								</view>
							</view>
						</view>
						<view class="popup_comment_title">全部评论</view>
						<view class="comment_list_box">
							<view class="comment_list" v-for="(item,index) in popupCommentList" :key="index">
								<image :src="item.userImg" mode="aspectFill"></image>
								<view class="comment_info">
									<view class="name_like">
										<view class="name">{{item.userName}}</view>
										<view class="like" @click="onLike(item,false)" :class="{active:item.isLike}">{{item.numLike || 0}}</view>
									</view>
									<view class="comment_content">{{item.content}}</view>
									<view class="time_reply">
										<view class="time">{{item.createTime}}</view>
										<!-- <view class="reply">{{item.replyCount}}回复</view> -->
									</view>
								</view>
							</view>
							<view class="no_data" v-if="popupCommentList.length <= 0">暂无数据</view>
						</view>
					</view>
				</scroll-view>
			</view>
		</z-popup>
	</view>
</template>
<script>
	import jyfParser from '@/components/common/jyf-parser/jyf-parser.vue';
	import zPopup from '@/components/common/popup.vue';
	import { appShare, 
		publicShare
	} from '@/config/utils';
	let scrollTop = 0;
	export default {
		components: {
			jyfParser,
			zPopup
		},
		data() {
			return {
				newsInfo: {},
				objId: "",
				popupShow: false,
				pageNo: 1,
				pages: 1,
				commentCount: 0,
				commentList: [],
				sendContent: "",
				replyPageNo: 1,
				replyPages: 1,
				replyCount: 0,
				popupCommentList: [],
				popupCommentInfo: {},
				placeholder: "写评论..."
			};
		},
		//第一次加载
		onLoad(e) {
			if (e.objId) {
				this.objId = e.objId;
			}
			this.pageData();
			this.loadData();
		},
		//页面显示
		onShow() {},
		//方法
		methods: {
			onShare() {
				appShare(this.newsInfo.share);
			},
			pageData() {
				this.$http
					.get('api/article/v1/info', {
						objId: this.objId,
					})
					.then(res => {
						this.newsInfo = res;
						this.$refs.article.setContent(res.content);
						// #ifdef H5
						publicShare(res.share);
						// #endif
					});
			},
			loadData() {
				if (this.pageNo <= this.pages) {
					let httpData = {
						pageNo: this.pageNo,
						pageSize: 15,
						objId: this.objId
					};
					this.$http
						.post('api/article/v1/comment_list', httpData, {
							load: false
						})
						.then(res => {
							uni.stopPullDownRefresh();
							this.pages = res.pages;
							this.commentCount = res.count;
							if (this.pageNo == 1) {
								this.commentList = res.data;
							} else {
								this.commentList = this.commentList.concat(res.data);
							}
							if (res.data.length > 0) {
								this.pageNo = this.pageNo + 1;
							}
						})
						.catch(err => {
							uni.stopPullDownRefresh();
						});
				}
			},
			// 发布评论
			onSendComment() {
				if (this.sendContent == "") {
					uni.showToast({
						title: "请输入评论内容",
						icon: "none"
					});
					return;
				}
				let httpData = {
					objId: this.objId,
					content: this.sendContent,
					addType: true
				};
				if (this.popupShow) {
					httpData.addType = false;
					httpData.objId = this.popupCommentInfo.objId;
				}
				this.$http
					.post('api/article/v1/add_comment', httpData)
					.then(res => {
						uni.showToast({
							title: "评论成功！"
						});
						this.sendContent = "";
						if (this.popupShow) {
							this.replyPageNo = 1;
							this.replyPages = 1;
							this.getReplyList();
						} else {
							this.pageNo = 1;
							this.pages = 1;
							this.loadData();
						}
					});
			},
			// 点赞
			onLike(item, type) {
				this.$http
					.get('api/article/v1/like', {
						objId: item.objId,
						type: type
					})
					.then(res => {
						if(type){
							this.newsInfo.isLike = !this.newsInfo.isLike;
						}else{
							if (this.popupShow) {
								this.onReply();
							} else {
								this.pageNo = 1;
								this.pages = 1;
								this.loadData();
							}
						}
					});
			},
			// 点击回复
			onReply(item) {
				if (item) {
					this.popupCommentInfo = item;
				}
				this.$http
					.get('api/article/v1/comment', {
						commentId: this.popupCommentInfo.objId
					})
					.then(res => {
						this.placeholder = "回复：" + res.userName;
						this.popupCommentInfo = res;
						this.popupShow = true;
					});
				this.replyPageNo = 1;
				this.replyPages = 1;
				this.getReplyList();
			},
			// 获取评论回复列表
			getReplyList() {
				if (this.replyPageNo <= this.replyPages) {
					let httpData = {
						pageNo: this.replyPageNo,
						pageSize: 15,
						objId: this.popupCommentInfo.objId
					};
					this.$http
						.post('api/article/v1/reply_info', httpData, {
							loadMore: false
						})
						.then(res => {
							uni.stopPullDownRefresh();
							this.replyPages = res.pages;
							this.replyCount = res.count;
							if (this.replyPageNo == 1) {
								this.popupCommentList = res.data;
							} else {
								this.popupCommentList = this.popupCommentList.concat(res.data);
							}
							if (res.data.length > 0) {
								this.replyPageNo = this.replyPageNo + 1;
							}
						})
						.catch(err => {
							uni.stopPullDownRefresh();
						});
				}
			},
			onPopupHide() {
				this.placeholder = "写评论...";
			},
			onSlideComment() {
				uni.createSelectorQuery().select('#commentTitle').boundingClientRect(function(rect) {
					uni.pageScrollTo({
						scrollTop: scrollTop + rect.top,
						duration: 300
					});
				}).exec()
			}
		},
		//页面隐藏
		onHide() {},
		//页面卸载
		onUnload() {},
		onPageScroll(e){
			scrollTop = e.scrollTop;
		},
		//页面下来刷新
		onPullDownRefresh() {},
		//页面上拉触底
		onReachBottom() {
			this.loadData();
		},
		//用户点击分享
		onShareAppMessage(e) {
			return this.wxShare();
		}
	};
</script>
<style lang="scss" scoped>
	@import '@/style/mixin.scss';

	.page {
		background-color: #FFF;
		padding: 30rpx 30rpx 130rpx 30rpx;
	}

	.nav_right_info {
		display: flex;
		padding-right: 30rpx;

		.share {
			width: 60rpx;
			height: 60rpx;
			@include bis('../../static/icon/ic_share_black.png', 60% 60%);
			margin-left: 25rpx;
			background-color: transparent;
			border-radius: 50%;

			&.black {
				background-color: rgba($color: #000000, $alpha: 0.2);
			}
		}
	}

	.title {
		font-size: 48rpx;
		color: #333333;
		font-weight: bold;
	}

	.news_info {
		display: flex;
		margin-top: 20rpx;
		font-size: 24rpx;
		color: #999999;
		display: flex;
		align-items: center;
		justify-content: space-between;

		.left_info {
			display: flex;
			align-items: center;

			text {
				display: flex;
				align-items: center;
			}

			.browse_num {
				margin-left: 50rpx;

				&::before {
					content: "";
					width: 37rpx;
					height: 21rpx;
					@include bis("../../static/icon/new_eye2.png");
					margin-right: 10rpx;
				}
			}
		}
	}

	.content {
		margin-top: 30rpx;
		font-size: 30rpx;
		color: #333333;
		line-height: 180%;

		image {
			width: 100%;
		}
	}

	.comment_title {
		font-size: 32rpx;
		color: #333;
		padding-top: 30rpx;
	}

	.popup_comment_title {
		font-size: 24rpx;
		color: #999999;
		padding: 30rpx 30rpx 0rpx 30rpx;
		border-top: 2rpx solid #f7f7f7;
	}

	.comment_list_box {
		margin-top: 23rpx;

		.comment_list {
			display: flex;
			align-items: flex-start;
			padding: 23rpx 0;

			>image {
				width: 72rpx;
				height: 72rpx;
				flex-shrink: 0;
				border-radius: 50%;
				margin-right: 24rpx;
			}

			.comment_info {
				flex: 1;

				.name_like {
					display: flex;
					align-items: center;
					justify-content: space-between;

					.name {
						font-size: 28rpx;
						color: #f25151;
					}

				}

				.like {
					font-size: 24rpx;
					color: #333333;
					display: flex;
					align-items: center;

					&::before {
						content: "";
						width: 32rpx;
						height: 28rpx;
						margin-right: 11rpx;
						@include bis("../../static/icon/ic_up.png");
					}

					&.active {
						color: #f25151;

						&::before {
							background-image: url("../../static/icon/ic_up_c.png");
						}
					}

				}

				.comment_content {
					margin-top: 20rpx;
					font-size: 32rpx;
					color: #333333;
				}

				.time_box {
					margin-top: 20rpx;
					font-size: 24rpx;
					color: #333333;
				}

				.like_reply {
					margin-top: 20rpx;
					display: flex;
					align-items: center;
					justify-content: space-between;

					.reply_box {
						display: flex;
						align-items: center;

						.image {
							width: 40rpx;
							height: 40rpx;
							border-radius: 50%;
							margin-right: 12rpx;
							flex-shrink: 0;
						}

						text {
							font-size: 24rpx;
							color: #333333;
						}

						.arrow {
							width: 14rpx;
							height: 24rpx;
							margin-left: 12rpx;
						}
					}
				}

				.time_reply {
					margin-top: 20rpx;
					display: flex;
					align-items: center;

					.time {
						font-size: 24rpx;
						color: #333333;
						margin-right: 25rpx;
					}

					.reply {
						height: 46rpx;
						background-color: #f7f7f7;
						border-radius: 23rpx;
						font-size: 24rpx;
						color: #333333;
						line-height: 46rpx;
						padding: 0 20rpx;
					}
				}
			}
		}
	}

	.comment_send_box {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		background-color: #FFF;
		display: flex;
		align-items: center;
		padding: 13rpx 20rpx 13rpx 30rpx;
		z-index: 600;

		.send_input_box {
			height: 62rpx;
			background-color: #f7f7f7;
			border-radius: 31rpx;
			border: solid 2rpx #e5e5e5;
			flex: 1;
			display: flex;
			align-items: center;
			padding: 0 30rpx;

			image {
				width: 27rpx;
				height: 30rpx;
			}

			input {
				height: 62rpx;
				margin-left: 20rpx;
				font-size: 28rpx;
			}
		}

		.icon_box {
			width: 60rpx;
			height: 60rpx;
			padding: 10rpx;
			position: relative;
			flex-shrink: 0;
			margin-left: 40rpx;

			image {
				width: 100%;
				height: 100%;
			}

			text {
				position: absolute;
				top: 0rpx;
				right: 10rpx;
				transform: translateX(50%);
				height: 24rpx;
				background-color: #f25151;
				border-radius: 10rpx;
				line-height: 20rpx;
				font-size: 18rpx;
				color: #ffffff;
				padding: 0 10rpx;
			}
		}
	}

	.reply_popup_box {
		background-color: #FFF;
		position: relative;

		.popup_scroll {
			max-height: 82vh;

			.popup_scroll_view {
				padding-bottom: 120rpx;
			}
		}

		.comment_list_box {
			padding: 0 30rpx;
		}

		.reply_popup_title {
			font-size: 36rpx;
			color: #333333;
			text-align: center;
			padding: 30rpx 0;
		}

		.reply_popup_close {
			position: absolute;
			top: 30rpx;
			right: 30rpx;
			width: 30rpx;
			height: 30rpx;

			image {
				width: 100%;
				height: 100%;
			}
		}
	}

	.no_data {
		height: 60rpx;
		line-height: 60rpx;
		text-align: center;
		font-size: 24rpx;
		color: #999;
	}
</style>
